package com.wiwf.model.mapper.config;

import javax.annotation.Resource;
import javax.sql.DataSource;

import com.wiwf.model.utils.DataSourceUtil;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.Assert;

import com.gitee.fastmybatis.core.FastmybatisConfig;
import com.gitee.fastmybatis.core.ext.SqlSessionFactoryBeanExt;

/**
 * @author kehui
 */
@Configuration
@MapperScan(basePackages = {"com.wiwf.model.mapper.admin.dao"}, sqlSessionFactoryRef = FastmybatisAdminCfg.SQL_SESSION_FACTORY_NAME)
public class FastmybatisAdminCfg {

	// private static final Logger logger = LoggerFactory.getLogger(FastmybatisAdminCfg.class);

	@Resource(name = DataSourceAdminBean.NAME)
	private DataSource dataSource;
	@Resource
	private FastmybatisConfig fastmybatisConfig;

	public static final String BASE_PACKAGE = "com.wiwf.model.mapper.admin.dao";
	/** mybatis的config文件路径 */
	public static final String MYBATIS_CONFIG_LOCATION = "classpath:mybatis/mybatisConfig.xml";
	/** mybatis的mapper文件路径 */
	public static final String MYBATIS_MAPPER_LOCATIONS = "classpath:mybatis/admin/*.xml";

	/** 数据源名称 */
	/** sqlSessionTemplate名称 */
	public static final String SQL_SESSION_TEMPLATE_NAME = "sst_" + DataSourceAdminBean.NAME;
	/** sqlSessionFactory名称 */
	public static final String SQL_SESSION_FACTORY_NAME = "ssf_" + DataSourceAdminBean.NAME;
	/** transactionManager名称 */
	public static final String TRANSACTION_MANAGER_NAME = "tm_" + DataSourceAdminBean.NAME;
	/** transactionTemplate名称 */
	public static final String TRANSACTION_TEMPLATE_NAME = "tt_" + DataSourceAdminBean.NAME;
	/* **************************************************** */

	@Bean(name = SQL_SESSION_FACTORY_NAME)
	SqlSessionFactory sqlSessionFactory() throws Exception {
		Assert.notNull(dataSource, "dataSource can not be null.");

		SqlSessionFactoryBeanExt bean = new SqlSessionFactoryBeanExt();

		bean.setDataSource(dataSource);
		bean.setConfigLocation(DataSourceUtil.getResource(MYBATIS_CONFIG_LOCATION));
		bean.setMapperLocations(DataSourceUtil.getResources(MYBATIS_MAPPER_LOCATIONS));

		bean.setBasePackage(BASE_PACKAGE);
		bean.setConfig(fastmybatisConfig);

		return bean.getObject();
	}

	@Bean(name = SQL_SESSION_TEMPLATE_NAME)
	SqlSessionTemplate sqlSessionTemplate(
			@Autowired @Qualifier(SQL_SESSION_FACTORY_NAME) SqlSessionFactory sessionFactory) throws Exception {
		return new SqlSessionTemplate(sessionFactory);
	}

	@Bean(name = TRANSACTION_MANAGER_NAME)
	PlatformTransactionManager annotationDrivenTransactionManager() {
		return new DataSourceTransactionManager(dataSource);
	}

	@Bean(name = TRANSACTION_TEMPLATE_NAME)
	TransactionTemplate transactionTemplate(
			@Autowired @Qualifier(TRANSACTION_MANAGER_NAME) PlatformTransactionManager transactionManager) {
		return new TransactionTemplate(transactionManager);
	}

}
